package com.wonders.frame.autoGeneration.task;

import com.wonders.frame.autoGeneration.support.TaskContent;
import com.wonders.frame.autoGeneration.support.enums.ColumnTypeEnum;
import com.wonders.frame.autoGeneration.task.Core.Task;
import com.wonders.frame.common.util.FileUtil;
import com.wonders.frame.common.util.HumpUtil;

import java.io.File;
import java.util.*;


/**
 * @author 顾乘瑞
 */
public class GenEntityTask extends Task {
    private static final String PATH = System.getProperty("file.separator") +"src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "java"
            + System.getProperty("file.separator") + "com"
            + System.getProperty("file.separator") + "wonders"
            + System.getProperty("file.separator") + "frame"
            + System.getProperty("file.separator") + "entity"
            + System.getProperty("file.separator");

    private static final List<String> SKIP_COLUMN = Arrays.asList(
            "id",
            "removed",
            "crt_user",
            "crt_date",
            "upd_user",
            "upd_date",
            "ID",
            "REMOVED",
            "CRT_USER",
            "CRT_DATE",
            "UPD_USER",
            "UPD_DATE"
    );

    public GenEntityTask(final TaskContent cont) {
        super(cont);
    }

    @Override
    public Object call() {
        String tableName = cont.getTableName();
        String className = HumpUtil.tableName2ClassName(tableName);

        String filePath = TaskContent.PROJECT_PATH + PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println(filePath);
            file.mkdirs();
        }
        String writeFilePath = filePath + className + ".java";
        StringBuilder content = new StringBuilder("package com.wonders.frame.entity;\n" +
                "\n" +
                "\n" +
                "import com.wonders.frame.core.entity.EntityTemplate;\n" +
                "\n" +
                "import com.fasterxml.jackson.annotation.JsonFormat;\n" +
                "import javax.persistence.Column;\n" +
                "import javax.persistence.Entity;\n" +
                "import javax.persistence.Table;\n" +
                "import lombok.Data;\n" +
                "import java.util.Date;\n" +
                "\n" +
                "@SuppressWarnings(\"serial\")\n" +
                "@Data\n" +
                "@Entity\n");

        content.append("@Table(name = \"" + tableName + "\")\n");
        content.append("public class " + className + " extends EntityTemplate {\n");
        cont.getColumns().forEach(v -> {
            if (SKIP_COLUMN.contains(v.getColumn())) {
                return;
            }
            content.append("    //" + v.getComment() + "\n");
            //时间类型字段json序列化时添加格式
            if(Objects.equals(v.getType(), "date")){
                content.append("    @JsonFormat(pattern=\"yyyy-MM-dd\",timezone=\"GMT+8\")");
            }
            if(Objects.equals(v.getType(), "datetime")){
                content.append("    @JsonFormat(pattern=\"yyyy-MM-dd hh:mm:ss\",timezone=\"GMT+8\")\n");
            }
            content.append("    @Column(name = \"" + v.getColumn() + "\")\n" +
                    "    private " + ColumnTypeEnum.valueOf(v.getType().toUpperCase()).getJavaType() + " " + HumpUtil.lineToHump(v.getColumn()) + ";\n\n");

        });
        content.append("\n}");
        FileUtil.createFile(content.toString(), writeFilePath);
        return null;
    }
}
